//
//  HomeTableViewCell.m
//  KeleAppProject
//
//  Created by 周岑 on 2018/1/14.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "HomeTableViewCell.h"
#import <AVFoundation/AVFoundation.h>
#import "TagView.h"
#import "LogoNumberView.h"
#import "HomeModel.h"
@interface HomeTableViewCell()
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) YYTextLayout *layout;
@property (nonatomic, strong) UIButton *deleteBtn;
@property (nonatomic, strong) YYLabel *contentLb;
@property (nonatomic, strong) UIImageView *profileImg;
@property (nonatomic, strong) UILabel *name;
@property (nonatomic, strong) UILabel *time;
@property (nonatomic, strong) UILabel *phoneType;
@property (nonatomic, strong) TagView *tagView;
@property (nonatomic, strong) LogoNumberView *collection;
@property (nonatomic, strong) LogoNumberView *comment;
@property (nonatomic, strong) LogoNumberView *prase;

@property (nonatomic, copy) NSString *type;
@end

@implementation HomeTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style
              reuseIdentifier:(NSString *)reuseIdentifier
                         type:(NSString *)type{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = KLColor_C1;
        [self loadBackView];
        self.type = type;
    }
    return self;
}
- (void)loadBackView{
    UIView *backView = [[UIView alloc]init];
    backView.backgroundColor = [UIColor whiteColor];
    self.backView = backView;
    [self.contentView addSubview:backView];
    [backView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView).offset(-10*HEIGHT);
    }];
    
    // 头像
    UIImageView *proileImageView = [[UIImageView alloc]init];
    proileImageView.image = [[UIImage imageNamed:@"info_touxiang"] circleImage];
    [self.backView addSubview:proileImageView];
    self.profileImg = proileImageView;
    [proileImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backView).offset(15*WIDTH);
        make.left.equalTo(self.backView).offset(15*WIDTH);
        make.width.height.mas_equalTo(32*WIDTH);
    }];
    
    // 名字
    UILabel *nameLabel = [[UILabel alloc]init];
    nameLabel.text = @"罗珊珊";
    self.name = nameLabel;
    nameLabel.textColor = KLColor_B6;
    nameLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14*WIDTH];
    [self.backView addSubview:nameLabel];
    [nameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backView).offset(15*HEIGHT);
        make.left.equalTo(proileImageView.mas_right).offset(12*WIDTH);
    }];
    
    if ([self.type isEqualToString:@"haveChecked"]) {
        
        // 删除
        UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [deleteBtn setTitleColor:KLColor_A1 forState:UIControlStateNormal];
        [deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
        deleteBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13*WIDTH];
        [[deleteBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            
        }];
        self.deleteBtn = deleteBtn;
        [self.backView addSubview:deleteBtn];
        [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(nameLabel);
            make.right.equalTo(self.backView).offset(-15*WIDTH);
            make.width.mas_equalTo(27*WIDTH);
            make.height.mas_equalTo(18*HEIGHT);
        }];
    }
    
    // 时间
    UILabel *timeLabel = [[UILabel alloc]init];
    timeLabel.text = @"10-18  20:16";
    timeLabel.textColor = KLColor_B7;
    timeLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13*WIDTH];
    [self.backView addSubview:timeLabel];
    self.time = timeLabel;
    [timeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel);
        make.top.equalTo(nameLabel.mas_bottom);
    }];
    
    // 手机类型
    UILabel *phoneType = [[UILabel alloc]init];
    phoneType.text = @"iphone 6plus";
    phoneType.textColor = KLColor_B7;
    phoneType.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13*WIDTH];
    [self.backView addSubview:phoneType];
    self.phoneType = phoneType;
    [phoneType mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(timeLabel);
        make.left.equalTo(timeLabel.mas_right).offset(8*WIDTH);
    }];
    

}
- (void)loadSubviewsOnHomeCellWithType:(NSString *)type{
    
    
    // 显示文本排版结果
    YYLabel *textLabel = [YYLabel new];
    self.contentLb = textLabel;
    textLabel.text = @"";
//    textLabel.text = @"aaa发哈接口老司机大幅度混加班ddd费骄傲的部分环境都不会祭敖包 都发不放假的八分来得及不富兰克林打不附近的卡接口发你的克兰副科级你你快单开发你发了积分卡";
//    NSMutableAttributedString *text = [[NSMutableAttributedString alloc]initWithString:textLabel.text];
//    text.yy_color = KLColor_B6;
//    text.yy_font = [UIFont fontWithName:@"PingFangSC-Regular" size:16*WIDTH];
//    CGSize size = CGSizeMake(KLScreenW - 30*WIDTH, CGFLOAT_MAX);
//    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:size text:text];
//    textLabel.textLayout = layout;
//    self.layout = layout;
    [self.backView addSubview:textLabel];
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.profileImg.mas_bottom).offset(16*HEIGHT);
        make.left.equalTo(self.backView).offset(15*WIDTH);
        make.right.equalTo(self.backView).offset(-15*WIDTH);
//        make.height.mas_equalTo(layout.textBoundingSize.height);
    }];
    
 
    UIImageView *playView = [[UIImageView alloc]init];
    playView.userInteractionEnabled = YES;
    [self.backView addSubview:playView];
    playView.tag = 105;
    self.playView = playView;
    [playView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textLabel.mas_bottom).offset(12*HEIGHT);
        make.left.right.equalTo(self.backView);
        make.height.mas_equalTo(210*HEIGHT);
    }];
    
    
    // 代码添加playerBtn到imageView上
    self.playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.playBtn setImage:[UIImage imageNamed:@"home_icon_bofang"] forState:UIControlStateNormal];
    [self.playBtn addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    [playView addSubview:self.playBtn];
    [self.playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(playView);
        make.width.height.mas_equalTo(50);
    }];
    
    
    // 标签的界面
    TagView *tagView = [[TagView alloc]init];
    [self.backView addSubview:tagView];
    self.tagView = tagView;
    [tagView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(playView.mas_bottom);
        make.left.right.equalTo(self.backView);
        make.height.mas_equalTo(48*HEIGHT);
    }];
    
    // 图标+数字的界面
    LogoNumberView *collectNum = [[LogoNumberView alloc]init];
    [self.backView addSubview:collectNum];
    self.collection = collectNum;
    [collectNum mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tagView.mas_bottom);
        make.left.equalTo(self.backView).offset(30*WIDTH);
        make.width.mas_equalTo(88*WIDTH);
        make.bottom.equalTo(self.backView);
    }];
    
    
    LogoNumberView *commentNum = [[LogoNumberView alloc]init];
    [self.backView addSubview:commentNum];
    self.comment = commentNum;
    [commentNum mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(collectNum);
        make.left.equalTo(collectNum.mas_right);
        make.width.mas_equalTo(88*WIDTH);
        make.bottom.equalTo(self.backView);
    }];
    
    
    LogoNumberView *praseNum = [[LogoNumberView alloc]init];
    [self.backView addSubview:praseNum];
    self.prase = praseNum;
    [praseNum mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(collectNum);
        make.left.equalTo(commentNum.mas_right);
        make.width.mas_equalTo(88*WIDTH);
        make.bottom.equalTo(self.backView);
    }];
    
    LogoNumberView *shareNumView = [[LogoNumberView alloc]init];
    [self.backView addSubview:shareNumView];
    shareNumView.signalDelegate = [RACSubject subject];
    [shareNumView.signalDelegate subscribeNext:^(id x) {
        if (self.shareSignal) {
            [self.shareSignal sendNext:nil];
        }
    }];
    self.shareNum = shareNumView;
    [shareNumView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(collectNum);
        make.left.equalTo(praseNum.mas_right);
        make.width.mas_equalTo(88*WIDTH);
        make.bottom.equalTo(self.backView);
    }];
    
    [shareNumView setValuesWithDataDic:@{@"imageStr":@"home_icon_fenxiang",@"title":@"分享"}];
}


+ (CGFloat)getHeightWithData:(HomeModel *)model{
    
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc]initWithString:model.title];
    text.yy_color = KLColor_B6;
    text.yy_font = [UIFont fontWithName:@"PingFangSC-Regular" size:16*WIDTH];
    CGSize size = CGSizeMake(KLScreenW - 30*WIDTH, CGFLOAT_MAX);
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:size text:text];
    NSMutableArray *tagArray = [NSMutableArray array];
    for (NSDictionary *dic in model.labelInfos) {
        [tagArray addObject:dic[@"labelName"]];
    }
    CGFloat height = tagArray.count == 0 ? 345*HEIGHT:378*HEIGHT;
    return height + layout.textBoundingSize.height;
}

+ (CGFloat)getHeightWithLayout:(YYTextLayout *)layout{
    return layout.textBoundingSize.height;
}

//- (void)setIsShowDelete:(BOOL)isShowDelete{
//    self.deleteBtn.hidden = isShowDelete;
//}


- (void)setModel:(HomeModel *)model {
//    [self.backView removeAllSubviews];
    // 清除无法复用的
    
    [self removeSub];
    [self loadSubviewsOnHomeCellWithType:self.type];
    // 图片
    // 删除plaview
    [self.playView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"loading_bgView"]];
    [self.profileImg sd_setImageWithURL:[NSURL URLWithString:model.userInfo[@"icon"]] placeholderImage:[UIImage imageNamed:@"info_touxiang"]];
    self.playView.contentMode =UIViewContentModeScaleAspectFill;
    self.playView.clipsToBounds = YES;
    // 内容
    self.contentLb.text = model.title;
    // 更新
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc]initWithString:self.contentLb.text];
    text.yy_color = KLColor_B6;
    text.yy_font = [UIFont fontWithName:@"PingFangSC-Regular" size:16*WIDTH];
    CGSize size = CGSizeMake(KLScreenW - 30*WIDTH, CGFLOAT_MAX);
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:size text:text];
    self.contentLb.textLayout = layout;
    self.layout = layout;
    [self.contentLb mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(layout.textBoundingSize.height);
    }];
    
    self.name.text = model.userInfo[@"nickname"];
    self.time.text = [NSDate getDateWithTimeInterval:model.createTime type:@"MM-dd HH:mm"];
    self.phoneType.text = model.phoneName;
    // 标签
    NSMutableArray *tagArray = [NSMutableArray array];
    for (NSDictionary *dic in model.labelInfos) {
        [tagArray addObject:dic[@"labelName"]];
    }
    // 下面都要更新
    if (tagArray.count == 0) {
        [self.tagView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(15);
        }];
    }

    [self.tagView addTagViewWithDataArray:tagArray];
    // 点在数目
    [self.collection setValuesWithDataDic:@{@"imageStr":@"home_icon_shouchang",@"title":[NSString stringWithFormat:@"%@",model.collectionNum]}];
    [self.comment setValuesWithDataDic:@{@"imageStr":@"home_icon_pinglun",@"title":[NSString stringWithFormat:@"%@",model.commentNum]}];
    [self.prase setValuesWithDataDic:@{@"imageStr":@"home_icon_dianzan",@"title":[NSString stringWithFormat:@"%@",model.praiseNum]}];


}

- (void)removeSub{
    [self.contentLb removeFromSuperview];
    [self.playView removeFromSuperview];
    [self.playBtn removeFromSuperview];
    [self.collection removeFromSuperview];
    [self.tagView removeFromSuperview];
    [self.comment removeFromSuperview];
    [self.prase removeFromSuperview];
    [self.shareNum removeFromSuperview];
}



- (void)play:(UIButton *)sender {
    if (self.playBlock) {
        self.playBlock(sender);
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
